package net.mihome.app.ui;

import java.util.Calendar;
import java.util.Date;

import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;

public class MihomeActivisAddActivity extends BaseActivity {
	private EditText nameEditText;
	private EditText timeEditText; //日期控件 
	private EditText timeTimeEditText;  //时间空进
	private EditText peopel_numEditText;
	private EditText descEditText;
	private Button backButton;
	private Button quedingButton;
	private ProgressBar progressBar;
	
	private Handler handler;
	private final static int HANDLER_ADD_STOP=1;
	private final static  int HANDLER_ADD_START=3;
	private final static  int HANDLER_ADD_EXCEPTION=2;
	
	private int nowYear=2014;
	private int nowMonth=11;
	private int nowDay=1;
	private int nowHour=12;
	private int nowMinute=30;
	
	private AppContext appContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//初始化时间
		final Calendar c = Calendar.getInstance();
        nowYear = c.get(Calendar.YEAR);  
        nowMonth = c.get(Calendar.MONTH);  
        nowDay = c.get(Calendar.DAY_OF_MONTH);
        nowHour = c.get(Calendar.HOUR_OF_DAY);
        nowMinute = c.get(Calendar.MINUTE);

		setContentView(R.layout.mihome_activity_add);
		progressBar=(ProgressBar) findViewById(R.id.chat_room_head_progress);
		progressBar.setVisibility(View.GONE);
		appContext=(AppContext) getApplication();
		nameEditText=(EditText) findViewById(R.id.chat_room_name);
		timeEditText=(EditText) findViewById(R.id.chat_room_time);
		peopel_numEditText=(EditText) findViewById(R.id.chat_room_peiolenum);
		descEditText=(EditText) findViewById(R.id.chat_room_desc);
		backButton=(Button) findViewById(R.id.chat_room_back_btn);
		timeEditText.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new DatePickerDialog(MihomeActivisAddActivity.this, new DatePickerDialog.OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						nowYear=year;
						nowMinute=monthOfYear+1;
						nowDay=dayOfMonth;
						timeEditText.setText(nowYear+"-"+nowMonth+"-"+nowDay);
					}
				}, nowYear, nowMonth,nowDay).show();
			}
		});
		timeTimeEditText=(EditText) findViewById(R.id.chat_room_time_time);
		timeTimeEditText.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new TimePickerDialog(MihomeActivisAddActivity.this,new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						// TODO Auto-generated method stub
						nowHour=hourOfDay;
						nowMinute=minute;
						timeTimeEditText.setText(nowHour+":"+nowMinute);
					}
				}, nowHour, nowMinute, true).show();
			}
		});
		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MihomeActivisAddActivity.this.finish();
			}
		});
		quedingButton=(Button) findViewById(R.id.chat_room_add_btn);
		quedingButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String name=nameEditText.getText().toString();
				if(name==null||name.trim().equals("")){
					Toast.makeText(MihomeActivisAddActivity.this, "活动名称不能为空", Toast.LENGTH_SHORT).show();
					return ;
				}
				String desc=descEditText.getText().toString();
				String time=timeEditText.getText().toString();
				String people_num=peopel_numEditText.getText().toString();
				addRoom(name, desc, time, people_num);
			}
		});
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message arg0) {
				switch (arg0.what) {
				case HANDLER_ADD_EXCEPTION:
					progressBar.setVisibility(View.GONE);
					Toast.makeText(MihomeActivisAddActivity.this, "发生异常，添加失败", Toast.LENGTH_SHORT).show();
					break;
				case HANDLER_ADD_START:
					progressBar.setVisibility(View.VISIBLE);
					break;
				case HANDLER_ADD_STOP:
					progressBar.setVisibility(View.GONE);
					nameEditText.setText("");
					descEditText.setText("");
					timeEditText.setText("");
					peopel_numEditText.setText("");
					Toast.makeText(MihomeActivisAddActivity.this, "创建成功", Toast.LENGTH_SHORT).show();
					MihomeActivisAddActivity.this.finish();
					break;
				default:
					break;
				}
				return false;
			}
		});
	}
	public void addRoom(final String name,final String desc,final String time,final String peopel_num){
		new Thread(new Runnable() {
			@Override
			public void run() {
				handler.sendEmptyMessage(HANDLER_ADD_START);
				try {
					appContext.addChatRoom(name, desc, time, peopel_num);
					handler.sendEmptyMessage(HANDLER_ADD_STOP);
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					handler.sendEmptyMessage(HANDLER_ADD_EXCEPTION);
				}
			}
		}).start();
	}
}
